package org.jeecg.interceptor;

import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.dto.OnlineAuthDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.OnlineAuth;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.service.IOnlineBaseAPI;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

/* renamed from: org.jeecg.interceptor.a */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/interceptor/a.class */
public class OnlineInterceptor implements HandlerInterceptor {

    /* renamed from: b */
    private IOnlineBaseAPI f61b;

    /* renamed from: c */
    private ISysBaseAPI f62c;

    /* renamed from: d */
    private static final String f63d = "/online/cgform";

    /* renamed from: e */
    private static final String f64e = "/online/cgform/workorder/add";

    /* renamed from: a */
    private static final Logger f60a = LoggerFactory.getLogger(OnlineInterceptor.class);

    /* renamed from: f */
    private static final String[] f65f = {"/online/cgformInnerTableList", "/online/cgformErpList", "/online/cgformList", "/online/cgformTreeList", "/online/cgformTabList"};

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        OnlineAuth methodAnnotation;
        //String[] strArr1 = null;
        if (handler.getClass().isAssignableFrom(HandlerMethod.class) && (methodAnnotation = ((HandlerMethod) handler).getMethodAnnotation(OnlineAuth.class)) != null) {
            f60a.debug("===== online 菜单访问拦截器 =====");
            String m583a = m583a(request.getRequestURI().substring(request.getContextPath().length()));
            String value = methodAnnotation.value();
            String substring = m583a.substring(m583a.lastIndexOf(value) + value.length());
            f60a.debug("拦截请求(" + request.getMethod() + ")：" + m583a + CgformUtil.f233E);
            if (CgformUtil.f271aq.equals(value) && "DELETE".equals(request.getMethod())) {
                substring = substring.substring(0, substring.lastIndexOf("/"));
            }
            String parameter = request.getParameter("tabletype");
            if (this.f61b == null) {
                this.f61b = (IOnlineBaseAPI) SpringContextUtils.getBean(IOnlineBaseAPI.class);
            }
            String onlineErpCode = this.f61b.getOnlineErpCode(substring, parameter);
            List<String> arrayList = new ArrayList<String>();
            int length = f65f.length;
            for (int i = 0; i < length; i++) {
                //arrayList.add(strArr[i] + onlineErpCode);
            	arrayList.add(onlineErpCode);
            }
            if (this.f62c == null) {
                this.f62c = (ISysBaseAPI) SpringContextUtils.getBean(ISysBaseAPI.class);
            }
            OnlineAuthDTO onlineAuthDTO = new OnlineAuthDTO(JwtUtil.getUserNameByToken(request), arrayList, f63d);
            onlineAuthDTO.setOnlineWorkOrderUrl(f64e);
            if (!this.f62c.hasOnlineAuth(onlineAuthDTO)) {
                m582a(response, value);
                return false;
            }
            return true;
        }
        return true;
    }

    /* renamed from: a */
    private String m583a(String str) {
        String str2 = "";
        if (oConvertUtils.isNotEmpty(str)) {
            str2 = str.replace("\\", "/").replace("//", "/");
            if (str2.indexOf("//") >= 0) {
                str2 = m583a(str2);
            }
        }
        return str2;
    }

    /* renamed from: a */
    private void m582a(HttpServletResponse httpServletResponse, String str) {
        PrintWriter printWriter = null;
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.setHeader("auth", "fail");
        try {
            try {
                PrintWriter writer = httpServletResponse.getWriter();
                if ("exportXls".equals(str)) {
                    writer.print("");
                } else {
                    writer.print(JSON.toJSON(Result.error("无权限访问(操作)")));
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                f60a.error(e.getMessage());
                if (0 != 0) {
                    printWriter.close();
                }
            }
        } catch (Throwable th) {
            if (0 != 0) {
                printWriter.close();
            }
            throw th;
        }
    }
}
